package com.zhhe.linearList;

/*
 *Author:ZouHeng
 *Des:循环链表
 *Date:2020-12-09  11:04
 */
public class CirLinkedList<T> implements IMyList<T> {
    /**
     * 头节点
     */
    private Node<T> head;

    /**
     * 尾节点
     */
    private Node<T> tail;

    private int size;

    public CirLinkedList() {
        this.head = this.tail = null;
        this.size = 0;
    }

    @Override
    public T get(int index) {
        if (index < 0 || index > size) {
            try {
                throw new Exception("索引越界，无法获取元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Node<T> node = head;
        for (int i = 0; i < index; i++) {
            node = node.getNext();
        }
        return node.getValue();
    }

    @Override
    public void add(T t) {
        Node<T> node = new Node<>(t);
        if (size == 0) {
            head = node;
            head.setNext(head);
            tail = head;
        } else {
            tail.setNext(node);
            node.setNext(head);
            tail = node;
        }

        size++;
    }

    @Override
    public void add(int index, T t) {
        if (index < 0 || index > size) {
            try {
                throw new Exception("索引越界，无法插入元素");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Node<T> newNode = new Node<>(t);
        if (index == 0) {
            Node<T> node = head;
            head = newNode;
            newNode.setNext(node);
            size++;
        } else if (index == size){
            add(t);
        } else {
            Node<T> prevNode = head;
            for (int i = 0; i < index; i++) {
                if (i > 0) {
                    prevNode = prevNode.getNext();
                }
            }
            Node<T> node = prevNode.getNext();
            prevNode.setNext(newNode);
            newNode.setNext(node);
            size++;
        }
    }

    @Override
    public void remove(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception("索引越界，无法删除元素，链表的长度为" + size + "，删除的索引为" + index);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (index == 0) {
            //删除头节点
            if (size == 1) {
                head = tail = null;
                size = 0;
            } else {
                Node<T> node = head.getNext();
                head.setValue(node.getValue());
                head.setNext(node.getNext());
            }
        } else if (index == size - 1) {
            //删除尾节点
            if (size == 1) {
                head = tail = null;
                size = 0;
            } else {
                Node<T> node = head;
                for (int i = 0; i < size - 1; i++) {
                    node = node.getNext();
                }
                node.setNext(head);
                tail = node;
            }
        } else {
            Node<T> prevNode = head;    //删除节点的前一个节点
            Node<T> nextNode = null;    //删除节点的后一个节点
            for (int i = 0; i < index; i++) {
                if (i > 0) {
                    prevNode = prevNode.getNext();
                }
                nextNode = prevNode.getNext().getNext();
            }
            prevNode.setNext(nextNode);
        }
        size--;
    }

    @Override
    public void removeAll() {
        head = tail = null;
        size = 0;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        if (head == null && tail == null) {
            return true;
        }
        return false;
    }

    public void print(){
        if (isEmpty()) {
            System.out.println("单链表为空");
        } else {
            System.out.print("CirLinkedList的容量为:" + size + "  元素为:{");
            Node<T> node = head;
            for (int i = 0; i < size; i++) {
                System.out.print(node.getValue());
                node = node.getNext();
                if (i < size - 1) {
                    System.out.print(" , ");
                }
            }
            System.out.print("} , 尾节点指向的头节点的value为：" + node.getValue());
        }
    }

    public static void main(String[] args) {
        CirLinkedList<Integer> list = new CirLinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
//        list.add(5, 10);
        list.print();
        list.remove(4);
        System.out.println();
        list.print();
    }
}
